package com.example.demo.mbp.entity;

import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 查询条件项
 */
@Data
public class QueryCondition implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 字段名
    private String field;
    
    // 操作符（eq=等于, ne=不等于, gt=大于, ge=大于等于, lt=小于, le=小于等于, 
    // in=包含, notIn=不包含, like=模糊查询, leftlike=左模糊, rightlike=右模糊）
    private String operator;
    
    // 值
    private Object value;
    
    // 允许的操作符列表
    private static final List<String> ALLOWED_OPERATORS = Arrays.asList(
        "eq", "ne", "gt", "ge", "lt", "le", "in", "notin", "like", "leftlike", "rightlike"
    );
    
    /**
     * 验证条件是否有效
     */
    public boolean isValid() {
        if (!StringUtils.hasText(field) || !StringUtils.hasText(operator) || value == null) {
            return false;
        }
        
        // 验证操作符
        if (!ALLOWED_OPERATORS.contains(operator.toLowerCase())) {
            return false;
        }
        
        // 验证in和notin操作符的值必须是列表
        if (("in".equalsIgnoreCase(operator) || "notin".equalsIgnoreCase(operator)) 
            && !(value instanceof List)) {
            return false;
        }
        
        return true;
    }
    
    /**
     * 标准化查询条件（如转换为小写）
     */
    public void normalize() {
        if (operator != null) {
            operator = operator.toLowerCase();
        }
    }
}    